package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Division;
import com.sl.au.entity.DivisionManager;
import com.sl.au.entity.User;
import com.sl.au.service.DivisionManagerService;
import com.sl.au.service.DivisionService;
import com.sl.au.service.UserService;
import com.sl.utils.MsgHelper;

@RestController
@RequestMapping("/ui/divisionMng")
public class DivisionManagerController extends BaseController {
	@Autowired
	private DivisionService divisionService;
	@Autowired
	private DivisionManagerService divisionManagerService;

	@Autowired
	private UserService userService;

	@RequestMapping(value = "/doInfo/{id}", method = RequestMethod.GET)
	public MsgHelper updInfo(@PathVariable("id") String id) {
		try {
			DivisionManager findOne = divisionManagerService.getRepository().findOne(id);
			divisionManagerService.getRepository().save(findOne);
			return MsgHelper.success();
		} catch (Exception e) {
			return MsgHelper.failed();
		}

	}

	@RequestMapping(value = "/doInfo", method = RequestMethod.GET)
	public MsgHelper addInfo() {
		try {
			DivisionManager d = null;
			String id = request.getParameter("id");
			String userId = request.getParameter("userId");
			String divisionId = request.getParameter("divisionId");
			String phone = request.getParameter("phone");
			if (id == null) {
				d = new DivisionManager();

			} else {
				d = divisionManagerService.getRepository().findOne(id);
			}
			d.setDivision(divisionService.getRepository().findOne(divisionId));
			d.setManager(userService.getRepository().findOne(userId));
			d.setPhoneNumber(phone);
			divisionManagerService.getRepository().save(d);
			return MsgHelper.success();
		} catch (Exception e) {
			e.printStackTrace();
			return MsgHelper.failed();
		}

	}

	@RequestMapping(value = "/findByDivision/{divisionId}", method = RequestMethod.GET)
	public MsgHelper findByDivision(@PathVariable("divis"
			+ "ionId") String divisionId) {

		Division division = divisionService.getRepository().findOne(divisionId);
		List<DivisionManager> infos = divisionManagerService.findPageByDivison(division, 0, Integer.MAX_VALUE);
		if (infos!=null&&infos.size() > 0) {
			return MsgHelper.failed();
		} else {
			return MsgHelper.success();
		}

	}

	@RequestMapping(value = "/doInfo/{id}", method = RequestMethod.DELETE)
	public MsgHelper deleteInfo(@PathVariable("id") String id) {
		try {
			divisionManagerService.getRepository().delete(id);
			return MsgHelper.success();
		} catch (Exception e) {
			return MsgHelper.failed();
		}

	}

	@RequestMapping(value = "getpage", method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String divisionId = request.getParameter("divisionId");
		Division division = divisionService.getRepository().findOne(divisionId);

		List<DivisionManager> infos = divisionManagerService.findPageByDivison(division, start, length);
		long count = divisionManagerService.fondCountByDivision(division);
		map.put("data", infos);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	/**
	 * 显示区域树结构
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/initDivisionTree", method = RequestMethod.GET)
	public Map<String, Object> getAllNodes(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Division> node = divisionService.getAllNodes();
		List<Map<String, Object>> nodeMap = new ArrayList<Map<String, Object>>();
		nodeMap = divisionService.getNodeMap(node);
		map.put("code", 1);
		map.put("nodes", nodeMap);

		return map;
	}

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index(ModelAndView modelAndView) {

		List<User> users = userService.getRepository().findAll();
		List<Division> divisions = divisionService.getRepository().findAll();
		modelAndView = createLayoutView("divisionMng/index");
		modelAndView.addObject("users", users);
		modelAndView.addObject("divisions", divisions);
		return modelAndView;
	}
}
